<template>
  <div id="app">
    <form action="#">
      <h1>新增商品</h1>
      <div>
        <label>品牌名称:</label>
        <input type="text" v-model="brand" />
      </div>
      <div>
        <label>创立时间:</label>
        <input type="text" v-model="dayTime" />
      </div>
      <div>
        <button @click="submit">确定提交</button>
      </div>
    </form>
    <button class="btn">新增</button>
    <table class="tb">
      <tr>
        <th>编号</th>
        <th>品牌名称</th>
        <th>创立时间</th>
        <th>操作</th>
      </tr>
      <!-- 循环渲染的元素tr -->

      <tr v-for="(item,index) in list " :key="item.id">
        <td>{{item.id}}</td>
        <td>{{item.name}}</td>
        <td>{{item.time}}</td>
        <td>
          <button @click="dev(index)">删除</button>
        </td>
      </tr>
      <tr>
        <td colspan="4" v-show="isShow">没有数据咯~</td>
      </tr>
    </table>
  </div>
</template>

<script>
export default {
  data() {
    return {
      list: [{ id: 1, name: '奔驰', time: '2020-08-01' }],
      brand: '',
      dayTime: '',
      isShow: false,
      add: {
        id: 1,
        name: '',
        dayTime: '',
      },
    }
  },
  methods: {
    submit() {
      //需要自定义一个id让它自增
      this.add.id += 1
      //需要把input输入的值传入给表格里面相对应的值
      this.add.name = this.brand
      this.add.time = this.dayTime
      this.list.push(this.add)
      this.brand = ''
      this.dayTime = ''
      //要把id修改成最新的样子
      this.add = { id: this.add.id, name: '', time: '' }
    },
    dev(index) {
      this.list.splice(index, 1)
      alert('删除成功')
    },
  },
}
</script>

<style>
#app {
  width: 600px;
  margin: 10px auto;
}
.btn {
  border: 1px solid #ccc;
  margin-bottom: 10px;
  margin-top: 40px;
}

.tb {
  border-collapse: collapse;
  width: 100%;
}

.tb th {
  background-color: #0094ff;
  color: white;
}

.tb td,
.tb th {
  padding: 5px;
  border: 1px solid black;
  text-align: center;
}

.add {
  padding: 5px;
  border: 1px solid black;
  margin-bottom: 10px;
}
</style>